package com.xqb.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqb.common.constant.Constants;
import com.xqb.common.utils.DateUtils;
import com.xqb.common.utils.SecurityUtils;
import com.xqb.modules.domain.po.StdInteractionQuestion;
import com.xqb.modules.domain.po.StdInteractionReply;
import com.xqb.modules.domain.po.StdLikedRecord;
import com.xqb.modules.domain.vo.InteractionReplyVo;
import com.xqb.modules.mapper.StdInteractionQuestionMapper;
import com.xqb.modules.mapper.StdInteractionReplyMapper;
import com.xqb.modules.mapper.StdLikedRecordMapper;
import com.xqb.modules.service.IStdInteractionReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 互动问题的回答或评论Service业务层处理
 *
 * @author xqb
 * @date 2024-10-14
 */
@Service
public class StdInteractionReplyServiceImpl implements IStdInteractionReplyService {
    @Autowired
    private StdInteractionReplyMapper stdInteractionReplyMapper;
    @Autowired
    private StdInteractionQuestionMapper stdInteractionQuestionMapper;
    @Autowired
    private StdLikedRecordMapper stdLikedRecordMapper;

    /**
     * 查询互动问题的回答或评论
     *
     * @param id 互动问题的回答或评论主键
     * @return 互动问题的回答或评论
     */
    @Override
    public StdInteractionReply selectStdInteractionReplyById(Long id) {
        return stdInteractionReplyMapper.selectStdInteractionReplyById(id);
    }

    /**
     * 查询互动问题的回答或评论列表
     *
     * @param stdInteractionReply 互动问题的回答或评论
     * @return 互动问题的回答或评论
     */
    @Override
    public List<StdInteractionReply> selectStdInteractionReplyList(StdInteractionReply stdInteractionReply) {
        return stdInteractionReplyMapper.selectStdInteractionReplyList(stdInteractionReply);
    }

    /**
     * 新增互动问题的回答或评论
     *
     * @param stdInteractionReply 互动问题的回答或评论
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertStdInteractionReply(StdInteractionReply stdInteractionReply) {
        stdInteractionReply.setCreateTime(DateUtils.getNowDate());
        if (stdInteractionReply.getAnswerId() != null) {
            StdInteractionReply stdParentInteractionReply = stdInteractionReplyMapper.selectStdInteractionReplyById(stdInteractionReply.getAnswerId());
            stdParentInteractionReply.setReplyTimes(stdParentInteractionReply.getReplyTimes() + 1);
            stdInteractionReplyMapper.updateStdInteractionReply(stdParentInteractionReply);
        }
        StdInteractionQuestion stdInteractionQuestion = stdInteractionQuestionMapper.selectStdInteractionQuestionById(stdInteractionReply.getQuestionId());
        stdInteractionQuestion.setLatestAnswerId(stdInteractionReply.getUserId());
        stdInteractionQuestion.setAnswerTimes(stdInteractionQuestion.getAnswerTimes() + 1);
        stdInteractionQuestionMapper.updateStdInteractionQuestion(stdInteractionQuestion);
        return stdInteractionReplyMapper.insertStdInteractionReply(stdInteractionReply);
    }

    /**
     * 修改互动问题的回答或评论
     *
     * @param stdInteractionReply 互动问题的回答或评论
     * @return 结果
     */
    @Override
    public int updateStdInteractionReply(StdInteractionReply stdInteractionReply) {
        stdInteractionReply.setUpdateTime(DateUtils.getNowDate());
        return stdInteractionReplyMapper.updateStdInteractionReply(stdInteractionReply);
    }

    /**
     * 批量删除互动问题的回答或评论
     *
     * @param ids 需要删除的互动问题的回答或评论主键
     * @return 结果
     */
    @Override
    public int deleteStdInteractionReplyByIds(Long[] ids) {
        return stdInteractionReplyMapper.deleteStdInteractionReplyByIds(ids);
    }

    /**
     * 删除互动问题的回答或评论信息
     *
     * @param id 互动问题的回答或评论主键
     * @return 结果
     */
    @Override
    public int deleteStdInteractionReplyById(Long id) {
        return stdInteractionReplyMapper.deleteStdInteractionReplyById(id);
    }

    /**
     * 查询互动问题的回答
     *
     * @param stdInteractionReply 互动问题的回答
     * @return 结果
     */
    @Override
    public List<InteractionReplyVo> selectStdInteractionReplyAnswerList(StdInteractionReply stdInteractionReply) {
        stdInteractionReply.setAnswerId(0L);
        return stdInteractionReplyMapper.getStdInteractionReplyList(stdInteractionReply);
    }

    /**
     * 查询互动问题的回答的回复
     *
     * @param stdInteractionReply 互动问题的回答
     * @return 互动问题的回答的回复
     */
    @Override
    public List<InteractionReplyVo> selectStdInteractionReplyReplyList(StdInteractionReply stdInteractionReply) {
        List<InteractionReplyVo> interactionReplyVoList = new ArrayList<>();
        if (stdInteractionReply.getAnswerId() == 0 || stdInteractionReply.getAnswerId() == null) {
            return interactionReplyVoList;
        }
        interactionReplyVoList = stdInteractionReplyMapper.getStdInteractionReplyList(stdInteractionReply);
        Long userId = SecurityUtils.getUserId();
        if (userId != null&&interactionReplyVoList.size()>0) {
            List<Long> replyIds = interactionReplyVoList.stream()
                    .map(InteractionReplyVo::getId)
                    .collect(Collectors.toList());

            List<StdLikedRecord> likedRecords = stdLikedRecordMapper.selectList(
                    new LambdaQueryWrapper<StdLikedRecord>()
                            .eq(StdLikedRecord::getUserId, userId)
                            .in(StdLikedRecord::getBizId, replyIds)
                            .eq(StdLikedRecord::getBizType, Constants.INTERACTION_REPLY)
            );
            Map<Long, Boolean> likedMap = likedRecords.stream()
                    .collect(Collectors.toMap(StdLikedRecord::getBizId, record -> true, (existing, replacement) -> existing));
            for (InteractionReplyVo vo : interactionReplyVoList) {
                vo.setLiked(likedMap.getOrDefault(vo.getId(), false));
            }
        }
        return interactionReplyVoList;
    }

    /**
     * 点赞或取消点赞
     *
     * @param id 回复或评论id
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStdInteractionReplyLike(Long id) {
        boolean res = false;
        LambdaQueryWrapper<StdLikedRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        StdInteractionReply stdInteractionReply = stdInteractionReplyMapper.selectStdInteractionReplyById(id);
        lambdaQueryWrapper.eq(StdLikedRecord::getUserId, SecurityUtils.getUserId())
                .eq(StdLikedRecord::getBizId, id)
                .eq(StdLikedRecord::getBizType, Constants.INTERACTION_REPLY);
        StdLikedRecord selectStdLikedRecord = stdLikedRecordMapper.selectOne(lambdaQueryWrapper);
        if (selectStdLikedRecord != null) {
            stdLikedRecordMapper.deleteById(selectStdLikedRecord.getId());
            stdInteractionReply.setLikedTimes(stdInteractionReply.getLikedTimes() - 1);
        } else {
            StdLikedRecord stdLikedRecord = new StdLikedRecord();
            stdLikedRecord.setUserId(SecurityUtils.getUserId());
            stdLikedRecord.setBizId(id);
            stdLikedRecord.setBizType(Constants.INTERACTION_REPLY);
            stdLikedRecordMapper.insert(stdLikedRecord);
            stdInteractionReply.setLikedTimes(stdInteractionReply.getLikedTimes() + 1);
            res = true;
        }
        stdInteractionReplyMapper.updateStdInteractionReply(stdInteractionReply);
        return res;
    }
}
